package com.cnhaimei.com;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

/**
 * 横向ListView
 * @author jin001.wu@gmail.com <br>
 * @date 2011-9-7
 *
 */
public class HListView extends Gallery {
	
	MyTouchListner mMyTouchListner;

	/**
	 * @return the mMyTouchListner
	 */
	public MyTouchListner getmMyTouchListner() {
		return mMyTouchListner;
	}

	/**
	 * @param mMyTouchListner the mMyTouchListner to set
	 */
	public void setmMyTouchListner(MyTouchListner mMyTouchListner) {
		this.mMyTouchListner = mMyTouchListner;
	}

	public HListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public HListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public HListView(Context context) {
		super(context);
	}

//	@Override
//	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
//			float velocityY) {
////		return false;
//		float v = 0;
////		if (velocityX < 0 && velocityX < - 3000) {
////			velocityX = - 3000;
////		} else if (velocityX > 0 && velocityX > 3000) {
////			velocityX = 4000;
////		}
//		return super.onFling(e1, e2, v, velocityY);
//	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			onMyTouch(ev.getRawX(), ev.getRawY(), MyTouchListner.EVENT_DOWN);
			break;
		case MotionEvent.ACTION_MOVE:
			onMyTouch(ev.getRawX(), ev.getRawY(), MyTouchListner.EVENT_MOVE);
			break;
		case MotionEvent.ACTION_UP:
			onMyTouch(ev.getRawX(), ev.getRawY(), MyTouchListner.EVENT_UP);
			break;
		default:
			onMyTouch(ev.getRawX(), ev.getRawY(), MyTouchListner.EVENT_CANCEL);
			break;
		}
		return super.onTouchEvent(ev);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return super.onInterceptTouchEvent(ev);
	}

//	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
////		super.onScrollChanged(l, t, oldl, oldt);
//		Log.e("HListView", "t:" + t + " HListView:" + (oldt - t));
//		this.computeScroll()
//	}
	
//	public void computeScroll() {
//		super.computeScroll();
//		Log.e("HListView", " HListcomputeScrollView:");
//	}
//	
	public void onMyTouch(float x, float y, int ev) {
		if (mMyTouchListner != null) {
			mMyTouchListner.onMyTouchListner(x, y, ev);
		}
	}
	
	public static interface MyTouchListner {
		int EVENT_DOWN = 1;
		int EVENT_UP = 2;
		int EVENT_MOVE = 3;
		int EVENT_CANCEL = 4;
		void onMyTouchListner(float x, float y, int e);
	}
	
//	@Override
//	public boolean dispatchTouchEvent(MotionEvent ev) {
//		super.dispatchTouchEvent(ev);
//		
//		return false;
//	}
	
	private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
		return e2.getX() > e1.getX();
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		// return super.onFling(e1, e2, 0, velocityY);//方法一：只去除翻页惯性
		// return false;//方法二：只去除翻页惯性 注：没有被注释掉的代码实现了开始说的2种效果。
		int kEvent;
		if (isScrollingLeft(e1, e2)) {
			// Check if scrolling left
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		} else {
			// Otherwise scrolling right
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		onKeyDown(kEvent, null);
		return true;
	}
}
